<template>
  <!-- 树状布局 -->
  <el-row class="custom-tree-node">
    <el-col :span="20">{{ treeNode.name }}</el-col>
    <el-col :span="4">
      <el-row width="100%">
        <el-col :span="12">{{ treeNode.manager }}</el-col>
        <el-col :span="12">
          <!-- 下拉菜单框 -->
          <el-dropdown @command="handleCommand" trigger="click">
            <!-- 操作 -->
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <!-- 添加 -->
              <el-dropdown-item command="add">
                添加子部门
              </el-dropdown-item>
              <!-- 编辑 -->
              <el-dropdown-item v-if="!isRoot" command="edit">
                编辑子部门
              </el-dropdown-item>
              <!-- 删除 -->
              <el-dropdown-item v-if="!isRoot" command="del">
                删除子部门
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
// 导入删除部门接口和编辑接口
import { delCompany } from '@/api/department'
export default {
  props: {
    treeNode: {
      type: Object,
      require: true
    },
    isRoot: {
      type: Boolean,
      // 设置它的默认值
      default: false
    }
  },
  methods: {
    handleCommand (cmd) {
      switch (cmd) {
        case 'add':
          this.$emit('addDept', this.treeNode.id)
          break
        case 'edit':
          this.$emit('edit', this.treeNode.id)
          break
        // 删除部门事件
        case 'del':
          this.$confirm('您确认要删除吗?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消'
          })
            .then(async () => {
              await delCompany(this.treeNode.id)
              this.$emit('delDept')
              this.$message.success('删除成功!')
            })
            .catch(() => {
              this.$message.success('取消成功!')
            })
          break
      }
    }
  }
}
</script>

<style></style>
